import 'package:dots_indicator/dots_indicator.dart';
import 'package:flutter/material.dart';
import 'package:flutter_swiper_null_safety/flutter_swiper_null_safety.dart';
import '../../router/navigator_util.dart';
import '../widget/show_pic_widget.dart';

// ignore: import_of_legacy_library_into_null_safe
import 'dart:convert';

// ignore: must_be_immutable
class ShowBannerWidget extends StatelessWidget {
  List<BannerImageModel>? imageList;
  final double height;
  final EdgeInsetsGeometry? padding;
  final Size activeSize;
  final Size size;
  final Color activeColor;
  final Color color;
  final double radius;
  final bool autoPlay;
  final Function(int index)? onTap;

  ShowBannerWidget({
    @required this.imageList,
    this.height = 120,
    this.padding,
    this.activeColor = const Color.fromARGB(204, 255, 255, 255),
    this.color = const Color.fromARGB(100, 255, 255, 255),
    this.size = const Size.square(6.0),
    this.activeSize = const Size(15.0, 6.0),
    this.radius = 5,
    this.autoPlay = true,
    this.onTap,
  });

  @override
  Widget build(BuildContext context) {
    if (imageList == null || imageList!.length == 0) {
      imageList = [
        BannerImageModel(img: ""),
        BannerImageModel(img: ""),
      ];
    }
    return Container(
      height: height,
      width: double.infinity,
      padding: padding,
      child: Swiper(
        loop: imageList!.length > 1,
        onTap: (int index) {
          if (this.onTap != null) {
            this.onTap?.call(index);
          }
        },
        itemCount: imageList!.length,
        itemBuilder: (ctx, index) {
          return ShowPicWidget(
            imageList![index].url ?? "",
            height: height,
            radius: radius,
          );
        },
        pagination: imageList!.length <= 1
            ? null
            : SwiperCustomPagination(
                builder: (BuildContext context, SwiperPluginConfig config) {
                  return Container(
                    alignment: Alignment.bottomCenter,
                    child: DotsIndicator(
                      dotsCount: imageList!.length,
                      position: config.activeIndex.toDouble(),
                      reversed: false,
                      decorator: DotsDecorator(
                        size: size,
                        activeSize: activeSize,
                        color: color,
                        activeColor: activeColor,
                        activeShape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(radius)),
                      ),
                    ),
                  );
                },
              ),
        autoplay: autoPlay,
      ),
    );
  }
}

BannerImageModel bannerImageModelFromJson(String str) => BannerImageModel.fromJson(json.decode(str));

String bannerImageModelToJson(BannerImageModel data) => json.encode(data.toJson());

class BannerImageModel {
  BannerImageModel({
    this.id,
    this.img,
    this.sort,
    this.title,
    this.url,
    this.link,
  });

  int? id;
  String? img;
  int? sort;
  String? title;
  String? url;
  String? link;

  factory BannerImageModel.fromJson(Map<String, dynamic> json) => BannerImageModel(
        id: json["id"],
        img: json["img"],
        sort: json["sort"],
        title: json["title"],
        url: json["url"],
        link: json["link"],
      );

  Map<String, dynamic> toJson() => {
        "id": id,
        "img": img,
        "sort": sort,
        "title": title,
        "url": url,
        "link": link,
      };
}
